package com.qzdsoft.erpcloud.controller.stock;


import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.qzdsoft.erpcloud.domain.stock.StockOutbound;
import com.qzdsoft.erpcloud.domain.stock.StockStore;
import com.qzdsoft.erpcloud.service.com.CustomerSupplierService;
import com.qzdsoft.erpcloud.service.com.CustomerSupplierStatementService;
import com.qzdsoft.erpcloud.service.proj.MaterialService;
import com.qzdsoft.erpcloud.service.stock.StockOutBoundDetailService;
import com.qzdsoft.erpcloud.service.stock.StockOutBoundService;
import com.qzdsoft.erpcloud.service.stock.StockStoreService;
import com.qzdsoft.erpcloud.service.sys.Permission;
import com.qzdsoft.erpcloud.service.sys.SysLog;
import com.qzdsoft.erpcloud.vo.proj.MaterialInfo;
import com.qzdsoft.erpcloud.vo.proj.MaterialQueryInfo;
import com.qzdsoft.erpcloud.vo.stock.StockOutboundDetailEditInfo;
import com.qzdsoft.erpcloud.vo.stock.StockOutboundDetailInfo;
import com.qzdsoft.erpcloud.vo.stock.StockOutboundDetailQueryInfo;
import com.qzdsoft.erpcloud.vo.stock.StockOutboundDetailRemoveInfo;
import com.qzdsoft.utils.ExceptionUtil;
import com.qzdsoft.vo.LayTableResponse;
import com.qzdsoft.vo.Response;
import com.qzdsoft.vo.ResultEnum;
import com.qzdsoft.vo.proj.MaterialReviewStatus;


@Controller
@RequestMapping("/stock/outbounddetail")
public class StockOutBoundDetailController
{
    private static final Logger logger = LoggerFactory.getLogger(StockOutBoundDetailController.class);
    private static final String BASEPATH = "stock/outbounddetail";
    
    @Autowired
    StockOutBoundService stockOutBoundService;
    @Autowired
    private MaterialService materialService;
    @Autowired
    StockOutBoundDetailService stockOutBoundDetailService;
    
    @Autowired
    private CustomerSupplierStatementService customerSupplierStatementService;
    
    @Autowired
    private CustomerSupplierService customerSupplierService;
    @Autowired
    StockStoreService stockStoreService;
    
    @RequestMapping("/index")
    public ModelAndView index(Integer id)
    {
        ModelAndView mv = new ModelAndView();
        StockOutbound stockOutbound = stockOutBoundService.findOne(id);
        mv.addObject("stockOutbound",stockOutbound);
        mv.setViewName(BASEPATH + "/detail");
//        LayTableResponse<StockOutboundInfo> so = null;
        return mv;
    }
    
    /**
     * 跳转到页面
     * @return
     */
    @Permission(validate=false)
    @ResponseBody
    @RequestMapping("/order-details/{id}")
    public ModelAndView item(@PathVariable("id")Integer id,StockOutboundDetailQueryInfo queryinfo)
    {
        //查询物料信息
        MaterialQueryInfo info = new MaterialQueryInfo ();
        info.setIsBuy(false);//销售的物料
        info.setStatus(MaterialReviewStatus.HAS_REVIEW_CODE);//查询已经审核的物料
        List<MaterialInfo> materialInfos = materialService.selectBy(info);
        ModelAndView mv = new ModelAndView();
        mv.addObject("materials", materialInfos);
       
        //查询销售出库单
        StockOutbound stockOutbound = stockOutBoundService.findOne(id);
        mv.addObject("stockOutbound",stockOutbound);
        
        Integer companyid = stockOutbound.getCompanyId();
        Integer supplierid= stockOutbound.getCustomerId();
        
        //供应商结算方式
//        List<CustomerSupplierStatementInfo> customerSupplierStatements = customerSupplierStatementService.selectBy(companyid, supplierid);
//        mv.addObject("statements",customerSupplierStatements);
//        //默认结算方式
//        for(CustomerSupplierStatementInfo s:customerSupplierStatements) {
//            if(s.getIsDefault()==1) {
//                mv.addObject("defaultStatement",s);
//            }
//        }
//        
//        //税率
//        TypeInfo taxInfo = customerSupplierService.selectTaxInfo(companyid, supplierid);
//        mv.addObject("taxInfo",taxInfo);
        //仓库列表
        List<StockStore> stockStore = stockStoreService.selectByComId(companyid);
        mv.addObject("stockStore",stockStore);
        //明细列表
        List<StockOutboundDetailInfo> list = stockOutBoundDetailService.selectAll(queryinfo);
        mv.addObject("details",list);
        
        mv.setViewName(BASEPATH + "/detailItem");
        return mv;
    }
    
    /**
     * 查询物料信息
     * @param info
     * @return
     */
    @Permission(validate=false)
    @ResponseBody
    @RequestMapping("/materias")
    public LayTableResponse<MaterialInfo> list(MaterialQueryInfo info){
        info.setIsBuy(false);
        info.setStatus(MaterialReviewStatus.HAS_REVIEW_CODE);//查询已经审核的物料
        return materialService.selectPageBy(info);
    }

    /**
     * 物料选择页面
     * @return
     */
    @Permission(validate=false)
    @RequestMapping("/selectmaterials")
    public ModelAndView selectMaterial() {
        ModelAndView view = new ModelAndView();
        view.setViewName(BASEPATH+"/materials");
        return view;
    }
    
    @ResponseBody
    @SysLog(description="销售出库单详情删除")
    @RequestMapping("/details/remove")
    public Response<String> remove(@RequestBody StockOutboundDetailRemoveInfo info){
        try
        {
            logger.debug("销售出库单详情删除，ids：{}",info.getIds());
            return stockOutBoundDetailService.remove(info.getIds());
        }
        catch (Exception ex)
        {
            logger.error("编辑销售出库单明细错误，错误信息：{}", ExceptionUtil.getStackTrace(ex));
        }
        return new Response<String>(ResultEnum.ERROR);
    }
    
    
    /**
     * 销售出库单详情编辑保存
     * @return
     */
    @SysLog(description="销售出库单详情保存")
    @ResponseBody
    @RequestMapping("/edit-orderdetail")
    public Response<String> edit(@RequestBody StockOutboundDetailEditInfo info) {
       
        try
        {
            return stockOutBoundDetailService.edit(info);
        }
        catch (Exception ex)
        {
            logger.error("编辑销售出库单明细错误，错误信息：{}", ExceptionUtil.getStackTrace(ex));
        }
        return new Response<String>(ResultEnum.ERROR);
    }
}
